package com.joint.web.action.mp;

import com.fz.us.base.bean.BaseEnum;
import com.fz.us.base.bean.Pager;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.joint.base.service.UsersService;
import com.joint.base.util.StringUtils;
import com.joint.core.entity.Announce;
import com.joint.core.service.AnnounceService;
import com.joint.web.action.BaseController;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * Created by gcw on 2015/3/11.
 */

@Controller("mpAnnounce")
@RequestMapping("/mp")
public class AjaxAnnounceController extends BaseController {
    private static final long serialVersionUID = -5383463207248344967L;

    @Resource
    private AnnounceService announceService;
    @Resource
    private UsersService usersService;

    private List<Announce> announceList;

    private Announce announce;

    @RequestMapping(value = "/ajax-announce/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> list(int page,String pageKeyword){

        List<Map<String, Object>> dataRows = Lists.newArrayList();
        JSONObject data =new JSONObject();
        Map<String, Object> rmap;
        pager = new Pager();
        pager.setPageSize(10);
        pager.setPageNumber(page);
        pager.setOrderBy("createDate");
        pager.setOrderType(BaseEnum.OrderType.desc);
        Map<String, Object> map;
        map = Maps.newHashMap();
        map.put("state", new BaseEnum.StateEnum[]{BaseEnum.StateEnum.Enable});
        map.put("name", pageKeyword);
        pager = announceService.findByPagerAndCompany(pager,null,usersService.getCompanyByUser(),map);
        announceList = (List<Announce>) pager.getList();
        for(Announce announce:announceList){
            rmap=Maps.newHashMap();
            rmap.put("url",getPath()+"mp/ajax-linkman/read/"+announce.getId());
            rmap.put("name",announce.getName());
            rmap.put("content", announce.getContent());
            dataRows.add(rmap);
        }
        data.put("pageNum", pager.getPageNumber());//页数
        data.put("pageSize", dataRows.size());//记录数
        data.put("pageCount", pager.getPageCount());//总页数
        data.put("totalCount", pager.getTotalCount());//总记录数
        data.put("pageKeyword", pager.getKeyword()==null?"": pager.getKeyword());//查询
        data.put("dataRows", dataRows);//数据
        return ajaxHtmlAppResult(1,"",data);
    }

    public String read(){
        if(StringUtils.isEmpty(keyId)) return "index";
        announce = announceService.get(keyId);
        return "read";
    }

    public List<Announce> getAnnounceList() {
        return announceList;
    }

    public void setAnnounceList(List<Announce> announceList) {
        this.announceList = announceList;
    }

    public Announce getAnnounce() {
        return announce;
    }

    public void setAnnounce(Announce announce) {
        this.announce = announce;
    }
}
